﻿#pragma once
#include"HashTable.h"
namespace hlh
{
	template <class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<K, V>, MapKeyOfT, Hash>::iterator iterator;

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.insert(kv);
		}

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		bool erase(const K& key)
		{
			return _ht.Erase(key);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.second;
		}
	private:
		hash_bucket::HashTable<K, pair<K, V>, MapKeyOfT, Hash> _ht;
	};


	// 测试一
	void test_map1()
	{
		unordered_map<int, int> v;
		v.insert(make_pair(1, 1));
		v.insert(make_pair(3, 3));
		v.insert(make_pair(10, 10));
		v.insert(make_pair(2, 2));
		v.insert(make_pair(8, 8));
		unordered_map<int, int>::iterator it = v.begin();
		while (it != v.end())
		{
			cout << it->first << ":" << it->second << " ";
			++it;
		}
		cout << endl;

	}
	// 测试二
	void test_map2()
	{
		unordered_map<string, string> dict;
		dict.insert(make_pair("sort", ""));
		dict.insert(make_pair("left", ""));
		dict.insert(make_pair("right", "ұ"));

		for (auto& kv : dict)
		{
			kv.second += 'y';

			cout << kv.first << ":" << kv.second << endl;
		}
	}

}